<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Attractions\AttractionManagement\Repository\Attraction_booking;

use Plugin\Attractions\AttractionManagement\Model\Attraction_booking\AttractionUserBooking;
use Hyperf\Database\Model\Builder;
use App\Repository\IRepository;
use Hyperf\Collection\Arr;

/**
 * 预约订单表 Repository类
 */
class AttractionUserBookingRepository extends IRepository
{
   public function __construct(
        protected readonly AttractionUserBooking $model
    ) {}

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 预约订单编号
        if (isset($params['id']) && !empty($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 用户id
        if (isset($params['user_id']) && !empty($params['user_id'])) {
            $query->where('user_id', '=', $params['user_id']);
        }

        // 预约活动信息id
        if (isset($params['info_id']) && !empty($params['info_id'])) {
            $query->where('info_id', '=', $params['info_id']);
        }

        // 预约日期
        if (isset($params['booking_day']) && !empty($params['booking_day']) && is_array($params['booking_day']) && count($params['booking_day']) == 2) {
            $query->whereBetween(
                'booking_day',
                [ $params['booking_day'][0], $params['booking_day'][1] ]
            );
        }

        // 预约时间段
        if (isset($params['booking_time']) && !empty($params['booking_time'])) {
            $query->where('booking_time', 'like', '%'.$params['booking_time'].'%');
        }

        // 预约状态1已预约2已取消3已使用
        if (isset($params['status']) && !empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 
        if (isset($params['created_at']) && !empty($params['created_at']) && is_array($params['created_at']) && count($params['created_at']) == 2) {
            $query->whereBetween(
                'created_at',
                [ $params['created_at'][0], $params['created_at'][1] ]
            );
        }

        // 
        if (isset($params['updated_at']) && !empty($params['updated_at']) && is_array($params['updated_at']) && count($params['updated_at']) == 2) {
            $query->whereBetween(
                'updated_at',
                [ $params['updated_at'][0], $params['updated_at'][1] ]
            );
        }

        // 
        if (isset($params['deleted_at']) && !empty($params['deleted_at']) && is_array($params['deleted_at']) && count($params['deleted_at']) == 2) {
            $query->whereBetween(
                'deleted_at',
                [ $params['deleted_at'][0], $params['deleted_at'][1] ]
            );
        }

        return $query;
    }
}